package com.navi168.fwq21.friend;

import org.apache.mina.core.session.IoSession;
import org.za.communication.core.handler.SessionIdleHandler;
import org.za.communication.message.TextMessage;

import com.navi168.fwq21.R;
import com.navi168.fwq21.hyzl.ShowMap;
import com.navi168.fwq21.util.Constants;
import com.navi168.fwq21.util.SendPosition;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Sendmes extends android.app.Activity {
	private EditText jsz;
	private EditText mes_c;

	private Button sendButton;
	private Button noButton;

	private String postnum;
	private IoSession session = SessionIdleHandler.ioSession;
	private ProgressDialog progDialog;
	private String whoString;
	private int flag;
	private long message_id;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sendmes);
		Bundle bundle = Sendmes.this.getIntent().getBundleExtra("sendmeslist");
		findviews();
		jsz.setText(bundle.getString("postname"));
		postnum = bundle.getString("postnum");
		postnum = postnum.trim().replace(" ", ",");
		setlisters();
		SharedPreferences preferences = this.getSharedPreferences(
				Constants.SPF, Context.MODE_WORLD_READABLE);
		whoString = preferences.getString("mymoblie", "");

	}

	private void setlisters() {
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendthemessage();

			}
		});
		noButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Sendmes.this.finish();

			}
		});

	}

	protected void sendthemessage() {
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				send();
			}

		});
		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDialog.setIndeterminate(false);
		progDialog.setCancelable(true);
		progDialog.setMessage("消息发送中中...");
		progDialog.show();
		thread.start();
		
	}

	protected void send() {
		TextMessage textMessage = new TextMessage();
		textMessage.setKey(SessionIdleHandler.key);
		textMessage.setSender(whoString);
		textMessage.setReceiver(postnum);
		textMessage.setMessagecontent(mes_c.getText().toString().trim());
		session.write(textMessage);
		while (SessionIdleHandler.go) {
			flag = SessionIdleHandler.textreturn;
			message_id = SessionIdleHandler.message_id;
			SessionIdleHandler.go = false;
			handler.sendEmptyMessage(flag);
			break;
			
		}
		
	}

	private void findviews() {
		jsz = (EditText) findViewById(R.id.jsr);
		mes_c = (EditText) findViewById(R.id.xxnr);
		sendButton = (Button) findViewById(R.id.sendbutton);
		noButton = (Button) findViewById(R.id.cancebutton);
		progDialog = new ProgressDialog(this);

	}
	
	private void showToast(String showString) {
		Toast.makeText(getApplicationContext(), showString, Toast.LENGTH_SHORT)
				.show();
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				progDialog.dismiss();
				showToast("消息发送成功！");
				Intent intent = new Intent(Sendmes.this , FriendAct.class);
				startActivity(intent);
				Sendmes.this.finish();
			}
			if (msg.what == 1) {
				progDialog.dismiss();
				showToast("接收的号码有错！");				
			}
			if(msg.what == 2){
				progDialog.dismiss();
				showToast("发送错误，请稍候再试！");
			}
			
		}
	};

	
}
